<?php
namespace Admin\Controller;
use Think\Controller;
use Think\Page;

class ActiveController extends Controller {
	
	private $pageSize = 10;
	
    public function index(){
		$query = array( 'status'	=>	1 ); 
		
		$parm = I('get.');
		$p = empty($parm['p'])?1:$parm['p']; 
		
		if(!empty($parm['s_title'])){
			$query['title'] = array('like','%'.$parm['s_title'].'%'); 
		}
		
		$this->assign('parm',$parm); 
		
		$Pro = M('Active');
		$list = $Pro->where($query)->page($p.','.$this->pageSize)->order('starttime asc')->select();
		
		$this->assign('list',$list);
		
		$count      = $Pro->where($query)->count();
		$Page       = new Page($count,$this->pageSize);
		
		$show = $Page->showhtml();
		$this->assign('page',$show);
		 
		$this->display(); 
    }
	
	public function edit(){
		$id = I('get.id');
		if(!empty($id)){
			$data = M('Active')->where('id='.$id)->find();
			$this->assign('d',$data);
		}
		$this->display();
	}
	
	public function save(){
		$data = I('post.');
		
		if($data['id']){
			$image	= $this->updateimg('image',false);
			$map	= $this->updateimg('map',false);
			if($image) $data['image'] = $image;
			if($map) $data['map'] = $map;
		}else{
			$image	= $this->updateimg('image',true);
			$map	= $this->updateimg('map',true);
			$data['image'] = $image;
			$data['map'] = $map;
		}
		
		if($data['id']){
			$rs = M('Active')->where('id='.$data['id'])->save($data);
			if($rs){
				$this->success('数据更新完成',U('Active/index'));
			}else{
				$this->error('数据更新失败，请稍后重试!');
			}
		}else{
			$rs = M('Active')->data($data)->add();
			if($rs){
				$this->success('数据保存完成',U('Active/index'));
			}else{
				$this->error('数据保存失败，请稍后重试!');
			}
		}
	}
	
	function updateimg($filed,$must){
		$img = $_FILES[$filed];
		if(empty($img['name'])){
			if($must){
				$this->error('图片不能为空，请稍后重试!');
			} else {
				return false;	
			}
		}
		$info = pathinfo($img['name']);
		$newname = date('Y_m_d_His').'_'.mt_rand(10000,99999).'.'.$info['extension'];
		$path = '/Public/upload/';
		if(move_uploaded_file($img['tmp_name'],'.'.$path.$newname)){
			return $path.$newname;
		}else{ 
			$this->error('图片上传失败，请稍后重试!');
		}
	}
	
	public function del(){
		$id = I('get.id');
		$rs = M('Active')->where('id='.$id)->delete();
		if($rs){
			$this->success('数据删除完成',U('Active/index'));
		}else{
			$this->error('数据删除失败，请稍后重试!');
		}
	}
	
}
